package org.zhq.core;

import org.zhq.thread.NamedThreadFactory;

import java.util.concurrent.*;

/**
 * @author zhengquan
 * @date 2019/2/24
 */
public class SchedulerImpl implements Scheduler {
    private final ScheduledExecutorService scheduledExecutorService;
    private final ExecutorService deliveryPool;

    public SchedulerImpl(int poolSize) {
        this.deliveryPool = Executors.newFixedThreadPool(4, new NamedThreadFactory("Delivery-Thread-"));
        this.scheduledExecutorService = Executors.newScheduledThreadPool(poolSize, new NamedThreadFactory("Scheduler-Thread-"));
    }

    @Override
    public ScheduledFuture<?> schedule(Runnable runnable, long delay, TimeUnit unit) {
        return scheduledExecutorService.schedule(runnable, delay, unit);
    }

    @Override
    public void delivery(Runnable runnable) {
        deliveryPool.execute(runnable);
    }

    @Override
    public void close() {
        scheduledExecutorService.shutdownNow();
        deliveryPool.shutdownNow();
    }
}
